#Binary parser template

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class Dekaron_mesh(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(Dekaron_mesh,self).__init__("MESH")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self):
        
        return self.inFile.read_string(self.inFile.read_long())
    
    #=== Collision mesh? ===
    
    def parse_collision_vertices(self, meshName, numVerts):
        
        print "%d verts" %numVerts
        for i in xrange(numVerts):
            self.inFile.read_float(3)
            self.inFile.seek(28, 1)
            
    def parse_collision_faces(self, meshName, numFaces):
        
        for i in xrange(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
        
    def parse_collision_bones(self, meshName, numBones):
        
        boneName = self.read_name()
        
    def parse_collision_meshes(self, offset):
        
        self.inFile.seek(offset)
        numMesh = self.inFile.read_long()
        
        for i in xrange(numMesh):
            meshName = self.read_name()
            print meshName
            bbox = self.inFile.read_float(6)
            numVerts = self.inFile.read_long()
            self.parse_collision_vertices(meshName, numVerts)
            
            numFaces = self.inFile.read_long() / 3
            self.parse_collision_faces(meshName, numFaces)
            
            numBones = self.inFile.read_long()
            self.parse_collision_bones(meshName, numBones)
            
    #=== normal mesh? ===
        
    def parse_vertices(self, meshName, numVerts):
        
        print "%d vertices" %numVerts
        for i in xrange(numVerts):
            vx, vy, vz, nx, ny, nz, tu, tv = self.inFile.read_float(8)
            self.inFile.seek(24, 1)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            self.add_vert_uv(meshName, i, [tu, tv])
    
    def parse_faces(self, meshName, numFaces):
        
        faces = list(self.inFile.read_short(2))
        faceNum = 0
        for i in xrange(2, numFaces):
            faces.append(self.inFile.read_short())
            temp = faces[:]
            
            if not i % 2:
                temp.reverse()
                
            #if not (temp[0] == temp[1] or temp[1] == temp[2] or temp[2] == temp[0]):
            self.create_face(meshName, faceNum)
            self.add_face_verts(meshName, faceNum, temp)
            faceNum += 1
                
            faces.pop(0)
                         
    def parse_bones(self, meshName, numBones):
        
        for i in xrange(numBones):
            boneName = self.read_name()
        
    def parse_meshes(self, offset):
        
        self.inFile.seek(offset)
        numMesh = self.inFile.read_long()
        meshName = "temp"
        self.create_object(meshName)
        
        for i in xrange(numMesh):
            bbox = self.inFile.read_float(6)
            unkCount = self.inFile.read_long()
            for j in xrange(unkCount):
                unk = self.inFile.read_long(4)
                numIndex = self.inFile.read_long()
                if numIndex:
                    self.parse_faces(meshName, numIndex)
            
            numVerts = self.inFile.read_long()
            self.parse_vertices(meshName, numVerts)
            
            numBones = self.inFile.read_long()
            self.parse_bones(meshName, numBones)
            self.inFile.seek(4, 1)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        idstring, unk0, unk1, unk2, meshOff0, meshSize0, meshOff1, meshSize1, \
            meshOff2, meshSize2, meshOff3, meshSize3 = self.inFile.read_long(12)
        
        path = self.inFile.read_string(64, "\x00")
        self.parse_collision_meshes(meshOff0)
        #self.parse_unk_mesh(meshOff1)
        self.parse_meshes(meshOff3)
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = Dekaron_mesh(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "\x17\x11\x03\x20", "MESH", "Dekaron MESH"

if __name__ == '__main__':
    
    file1 = ""
    obj = read_file(file1)